package com.weiquan.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.weiquan.entity.ApiLogRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * API日志记录Mapper接口
 */
@Mapper
public interface ApiLogRecordMapper extends BaseMapper<ApiLogRecord> {
    
    /**
     * 获取平均响应时间
     */
    @Select("SELECT AVG(response_time) FROM api_log_record WHERE create_time >= #{startTime}")
    Double getAvgResponseTime(@Param("startTime") LocalDateTime startTime);
    
    /**
     * 获取最常访问的接口
     */
    @Select("SELECT request_url, COUNT(*) as count FROM api_log_record " +
            "WHERE create_time >= #{startTime} " +
            "GROUP BY request_url " +
            "ORDER BY count DESC " +
            "LIMIT #{limit}")
    List<Map<String, Object>> getTopApis(@Param("startTime") LocalDateTime startTime, @Param("limit") int limit);
    
    /**
     * 获取请求方法分布
     */
    @Select("SELECT request_method, COUNT(*) as count FROM api_log_record " +
            "WHERE create_time >= #{startTime} " +
            "GROUP BY request_method")
    List<Map<String, Object>> getMethodDistribution(@Param("startTime") LocalDateTime startTime);
}
